package xyz.xtt.asset.api.enums;

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author huqibo
 * 
 *         资产类型枚举
 */
public enum AssetTypeEnum {

	/**
	 * 地皮
	 */
	GROUND(0, "地皮", "ground", "land"),
	/**
	 * 经营证
	 */
	BIZ_CERTIFICATE(1, "经营证", "certificate", "operate"),
	/**
	 * 元宇宙资产
	 */
	METAVERSE(2, "元宇宙资产", "metaverse", ""),
	/**
	 * 聊天
	 */
	CHAT(3, "聊天", "chat", ""),
	/**
	 * 店铺
	 */
	SHOP(4, "店铺", "shop", ""),
	/**
	 * 商品
	 */
	GOODS(5, "商品", "goods", ""),
	/**
	 * NFT
	 */
	NFT(1000, "数字藏品", "nft", ""),
	/**
	 * 竞猜
	 */
	EXCEPT(6, "竞猜", "except", "nft"),
	/**
	 * 头像
	 */
	PFPS(7, "头像", "pfps", "nft"),
	/**
	 * 元素
	 */
	ELEMENT(8, "元素", "element", "nft"),
	/**
	 * 权益
	 */
	INTEREST(9, "权益", "interest", "nft"),

	/**
	 * 摄影
	 */
	PHOTO(10, "摄影", "photo", "nft"),
	/**
	 * 音乐
	 */
	MUSIC(11, "音乐", "music", "nft"),

	/**
	 * 设计
	 */
	DESIGN(12, "设计", "design", "nft"),

	/**
	 * 展示小老虎
	 */
	PLATFORM(13, "平台展示", "platform", "nft"),
	/**
	 * 展示礼包权益
	 */
	MEMBER(14, "会员展示", "member", "nft"),
	/**
	 * 展示礼包权益
	 */
	EXPLORER(15, "探索器", "explorer", "nft"),
	/**
	 * 亚洲小姐盲盒
	 */
	MISS_ASIA_BLIND_BOX(60, "亚洲小姐盲盒", "missAsia", "nft"),

	BLIND_BOX(61, "盲盒", "BlindBox", "nft");
	;

	private int code;
	private String desc;
	private String groupType;
	private String outMyAssetType;

	/**
	 * @param code
	 * @param desc
	 * @param groupType
	 */
	AssetTypeEnum(int code, String desc, String groupType, String outMyAssetType) {
		this.code = code;
		this.desc = desc;
		this.groupType = groupType;
		this.outMyAssetType = outMyAssetType;
	}

	public int code() {
		return code;
	}

	public String desc() {
		return desc;
	}

	public String groupType() {
		return groupType;
	}

	public String outMyAssetType() {
		return outMyAssetType;
	}

	public static boolean checkType(String checkType) {
		return Arrays.asList(AssetTypeEnum.values()).stream().anyMatch(x -> Objects.equals(x.name(), checkType));
	}

	public static AssetTypeEnum convert(String checkType) {
		return Arrays.asList(AssetTypeEnum.values()).stream().filter(x -> Objects.equals(x.name(), checkType.trim().toUpperCase())).findFirst().orElse(null);
	}

	public static AssetTypeEnum queryConvert(Integer checkType) {
		return Arrays.asList(AssetTypeEnum.values()).stream().filter(x -> Objects.equals(x.code(), checkType)).findFirst().orElse(null);
	}

	public static List<Integer> queryNftTypes() {
		return Arrays
				.asList(AssetTypeEnum.EXCEPT.code(), AssetTypeEnum.PFPS.code(), AssetTypeEnum.ELEMENT.code(), AssetTypeEnum.INTEREST.code(), AssetTypeEnum.PHOTO
						.code(), AssetTypeEnum.MUSIC.code(), AssetTypeEnum.DESIGN.code(), AssetTypeEnum.PLATFORM.code(), AssetTypeEnum.MEMBER.code());
	}

	// 静态map方法
	static final Map<Integer, AssetTypeEnum> map = new HashMap<>();
	static {
		for (AssetTypeEnum value : AssetTypeEnum.values()) {
			map.put(value.code(), value);
		}
	}

	public static AssetTypeEnum getEnumByCode(int code) {
		return map.getOrDefault(code, null);
	}

	public static List<Integer> listQueryType(String filterAssetType) {
		boolean empty = ObjectUtils.isEmpty(filterAssetType);
		if (empty) {
			return new ArrayList<>();
		}
		String[] split = StringUtils.split(filterAssetType, ",");
		if (Objects.isNull(split)) {
			AssetTypeEnum convert = convert(filterAssetType);
			if (Objects.nonNull(convert)) {
				return Arrays.asList(convert.code());
			}
			return new ArrayList<>();
		}
		List<Integer> targetCodes = Arrays.stream(split).filter(x -> !ObjectUtils.isEmpty(x)).map(x -> convert(x)).map(AssetTypeEnum::code).collect(toList());
		return targetCodes;
	}
}
